package pers.qianyu.month_202102.date_20210201;

import java.util.*;

/**
 * 888. 公平的糖果棒交换
 * https://leetcode-cn.com/problems/fair-candy-swap/
 *
 * @author mizzle rain
 * @date 2021-02-01 15:40
 */
public class FairCandySwap {
    public int[] fairCandySwap(int[] A, int[] B) {
        int sumA = 0, sumB = 0;
        for (int x : A) sumA += x;
        for (int x : B) sumB += x;
        int target = sumA - sumB >> 1;
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < A.length; i++) {
            map.put(A[i], i);
        }
        for (int x : B) {
            int t = target + x;
            if (map.containsKey(t)) {
                return new int[]{t, x};
            }
        }
        return new int[]{-1, -1};
    }
}
